<?php
/**
 * Model for a PHP Copy/Paste Detector (CPD)  log.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Model for a PHP Copy/Paste Detector (CPD) log.
 *
 * @package PhpCilux
 */
class Model_Log_PhpCpd extends Model_Log_Abstract
{

    /**
     * Constructor.
     *
     * @param string $projectKey The project key.
     * @param string $buildKey   The build key.
     */
    public function __construct($projectKey, $buildKey)
    {
        parent::__construct($projectKey, $buildKey);

        $this->_logFilename = 'phpcpd.txt';
    }

    /**
     * Get the status of the PHP Unit log.
     *
     * @return string
     */
    public function status()
    {
        $log        = $this->getContent();
        $lines      = explode("\n", $log);
        $totalLines = count($lines);

        $line = $lines[($totalLines - 2)];
        $pos  = stripos($line, '%');
        $val  = (double) substr($line, 0, $pos);

        if (0 == $val) {
            return 'success';
        } elseif (10 > $val) {
            return 'warning';
        }

        return 'failed';
    }

    /**
     * Formating the log.
     *
     * @param string $content The log content which will be styled.
     *
     * @return string
     */
    public function format($content)
    {
        $lines = explode("\n", $content);

        $content = '<h4>PHP Copy/Paste Detector</h4>';

        $totalLines = count($lines);

        while (list($key, $line) = each($lines)) {
            $line = rtrim($line);

            switch(true) {
                case 0 == $key:
                case '' == trim($line):
                case $key == ($totalLines - 1):
                    break;

                case $key == ($totalLines - 2):
                    $pos = stripos($line, '%');
                    $val = (double) substr($line, 0, $pos);

                    if (0 == $val) {
                        $content .= '<div class="success"><pre>' . $line . '</pre></div>';
                    } elseif (10 > $val) {
                        $content .= '<div class="warning"><pre>' . $line . '</pre></div>';
                    } elseif (10 < $val) {
                        $content .= '<div class="failed"><pre>' . $line . '</pre></div>';
                    } else {
                        $content .= '<pre>' . $line . '</pre>';
                    }
                    break;

                default:
                    $content .= '<pre>' . $line . '</pre>';
                    break;
            }
        }

        $content .= '<br />';

        return $content;
    }

}